package router

import (
	"yunj/app/controller/admin"
	"yunj/app/controller/admin/demo"
	"yunj/app/controller/admin/demo/form"
	"yunj/app/controller/admin/demo/table"
	"yunj/app/controller/admin/demo/yimport"
	"yunj/pkg/global"
	adminMiddleware "yunj/pkg/yunj/app/middleware/admin"

	"github.com/gin-gonic/gin"
)

// 后台路由
func adminRouter(router *gin.Engine) {
	adminRouter := router.Group(global.Config.GetAppAdminEntrance())
	{
		// 错误捕获和日志中间件
		adminRouter.Use(
			gin.RecoveryWithWriter(gin.DefaultErrorWriter, func(c *gin.Context, recovered any) {
				adminMiddleware.Recover(c, recovered)
			}),
			adminMiddleware.Log(),
		)
		// 不需要登录鉴权
		r1 := *adminRouter
		{
			demoController := demo.Index{}
			// 示例页面
			r1.Any("/demo", demoController.Index)
		}

		// 需要登录鉴权
		r2 := *adminRouter
		{
			// 登录鉴权中间件
			r2.Use(adminMiddleware.Auth())
			// 欢迎页面
			welcomeRouter := r2.Group("/welcome")
			{
				welcomeController := admin.Welcome{}
				welcomeRouter.Any("", welcomeController.Index)
				welcomeRouter.POST("cache-refresh", welcomeController.CacheRefresh)
				welcomeRouter.POST("number-data", welcomeController.NumberData)
				welcomeRouter.POST("line-chart-data", welcomeController.LineChartData)
				welcomeRouter.POST("map-chart-data", welcomeController.MapChartData)
				welcomeRouter.POST("bar-chart-data", welcomeController.BarChartData)
			}

			// demo
			demoRouter := r2.Group("/demo")
			// 表单构建器
			demoFormRouter := demoRouter.Group("/form")
			{
				demoFormIndexController := form.Index{}
				// 示例
				demoFormRouter.Any("/index", demoFormIndexController.Index)
				// 下拉搜索可选项
				demoFormRouter.Any("/dropdownSearchOptions", demoFormIndexController.DropdownSearchOptions)
			}
			// 表格构建器
			demoTableRouter := demoRouter.Group("/table")
			{
				demoTableIndexController := table.Index{}
				demoTableRouter.Any("/index", demoTableIndexController.Index)
				// 树形表格
				demoTableRouter.Any("/tree", demoTableIndexController.Tree)
			}
			// 导入构建器
			demoImportRouter := demoRouter.Group("/import")
			{
				demoImportIndexController := yimport.Index{}
				// 示例
				demoImportRouter.Any("/index", demoImportIndexController.Index)
			}
		}
	}

}
